﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CustomerManagement_AddLineOfBalanceManagement : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("300400102");
        if(!IsPostBack)
        {
             btnCancel.Attributes.Add("onclick", "if(!confirm('要取消新建结算单并返回上一页吗?'))return false;");

         }

        Session["FCode"] = "300400102";
    }
    //private void BindType() 
    //{
    //    List<Model.BATYPE> type = new List<Model.BATYPE>();
    //    type = new BLL.BATYPE().GetAllList();
    //    DropType.DataSource = type;
    //    DropType.DataTextField = "BAT_NAME";
    //    DropType.DataValueField = "BAT_CODE";
    //    DropType.DataBind();
    //    
        
    //}

    private DataSet DstDetails
    {
        get
        {
            DataSet dstDetails = new BLL.BADETAIL().CreateTempDataTable();
            if (ViewState["DstDetails"] != null)
            {
                dstDetails = (DataSet)ViewState["DstDetails"];
            }
            return dstDetails;
        }
        set
        {
            ViewState["DstDetails"] = value;
        }
    }
    protected void dlistDetail_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "del")
        {
            if (this.DstDetails != null)
            {
                this.DstDetails.Tables[0].Rows.RemoveAt(e.Item.ItemIndex);
                dlistDetail.DataSource = this.DstDetails;
                dlistDetail.DataBind();
            }
        }

    }
    //新增结余款调整单明细
    protected void btnAddDetail_Click(object sender, EventArgs e)
    {
        try
        {
           // BindType();
            string customerCode = hidUserCode.Value;
            if (customerCode != "")
            {
                string[] customersArray = customerCode.Split(',');
                if (customersArray.Length > 0)
                {
                    string[] customer;
                    for (int i = 0; i < customersArray.Length; i++)
                    {
                        customer = customersArray[i].Split('|');
                        if (customer.Length == 3)
                        {
                            SetValueOfDataTable(this.DstDetails);
                            this.DstDetails = new BLL.BADETAIL().AddDataRowToDataTable(this.DstDetails, Convert.ToString(this.DstDetails.Tables[0].Rows.Count + 1), customer[0], customer[2],"");
                            dlistDetail.DataSource = this.DstDetails;
                            dlistDetail.DataBind();
                        }
                    }
                }
            }
            else
            {
                return;//Share.Alert(this.Page, "请选择会员！");
            }
            hidUserCode.Value = "";
        }
        catch (Exception ex)
        {
            Share.Alert(this.Page, ex.Message);
        }
    }
    private void SetValueOfDataTable(DataSet dst)
    {
        Label lblDetailId;
        Label lblCustomerCode;
        Label lblCustomerName;
        TextBox txtAmount;
        TextBox txtNote;

        for (int i = 0; i < dlistDetail.Items.Count; i++)
        {
            lblDetailId = (Label)dlistDetail.Items[i].FindControl("lblDetailId");
            lblCustomerCode = (Label)dlistDetail.Items[i].FindControl("lblCustomerCode");
            lblCustomerName = (Label)dlistDetail.Items[i].FindControl("lblCustomerName");
            txtAmount = (TextBox)dlistDetail.Items[i].FindControl("txtAmount");
            txtNote = (TextBox)dlistDetail.Items[i].FindControl("txtNote");

            dst.Tables[0].Rows[i]["BAD_DETAILCODE"] = lblDetailId.Text;
            dst.Tables[0].Rows[i]["BAD_CUSTOMERCODE"] = lblCustomerCode.Text;
            dst.Tables[0].Rows[i]["GKMC"] = lblCustomerName.Text;
            dst.Tables[0].Rows[i]["BAD_AMT"] = Convert.ToDecimal(txtAmount.Text.Trim());
            dst.Tables[0].Rows[i]["BAD_NOTE"] = txtNote.Text.Trim();
        }

    }
   
     //保存新增的结余款调整单及调整单明细      
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("300400102", "00017"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        try
        {
            SetValueOfDataTable(this.DstDetails);
            if (this.DstDetails.Tables[0].Rows.Count == 0)
            {
                throw new Exception("请添加会员！");

            }
                string newId = "";
                if (this.DropType.SelectedValue != "-1")
                {
                    string adjustType = DropType.SelectedValue.Trim();
                    if (new BLL.BALANCEADJUST().InsertNew(this.DstDetails, Share.UserValue(0), out newId, adjustType))
                    {

                        Response.Write("<script>alert('新增成功！');window.location.href='LineOfBalanceManagementDetails.aspx?code=" + newId + "'</script>");
                    }
                    else
                    {
                        Share.Alert(this.Page, "新增未成功，请重新添加！");
                    }
                } else { Share.Alert(Page, "请选择调整类型"); }
                
            }
           catch (Exception ex)
             {
               Share.Alert(this.Page, ex.Message);
             }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("LineOfBalanceManagement.aspx");
    }
    
}
